package com.yanqu.road.dao.impl.activity.fund;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.fund.UserActivityFundData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserActivityFundDataDaoImpl extends TempDataObjectDao<UserActivityFundData> {

    public static String getUserActivityFundDataSql = "select * from `t_u_activity_fund` where `userId`=?";

    public List<UserActivityFundData> getUserActivityFundDataList(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserActivityFundData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserActivityFundDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityFundData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String getUserDataSql = "select * from `t_u_activity_fund` where `activityId` = ? and `userId` = ?";
    public UserActivityFundData getUserActivityFundData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserActivityFundDataSql = "update `t_u_activity_fund` set buyMallKeyIds=?,progress=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserActivityFundData userActivityFundData) {
        userActivityFundData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userActivityFundData.getBuyMallKeyIds())));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityFundData.getProgress()));
        para.put(i++, new DbParameter(Types.INTEGER, userActivityFundData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityFundData.getUserId()));
        if (getDb().executeNoneQuery(updateUserActivityFundDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserActivityFundDataSql = "insert into `t_u_activity_fund`(activityId,userId,buyMallKeyIds,progress) values (?,?,?,?)";
    @Override
    protected boolean addData(UserActivityFundData userActivityFundData) {
        userActivityFundData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userActivityFundData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityFundData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userActivityFundData.getBuyMallKeyIds())));
        para.put(i++, new DbParameter(Types.BIGINT, userActivityFundData.getProgress()));
        if (getDb().executeNoneQuery(insertUserActivityFundDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserActivityFundData parseData(ResultSet rs) throws SQLException {
        UserActivityFundData userActivityFundData = new UserActivityFundData();
        userActivityFundData.setActivityId(rs.getInt("activityId"));
        userActivityFundData.setUserId(rs.getLong("userId"));
        userActivityFundData.setBuyMallKeyIds(JSON.parseObject(rs.getString("buyMallKeyIds"), new TypeReference<List<Integer>>() {}));
        userActivityFundData.setProgress(rs.getLong("progress"));

        return userActivityFundData;
    }
}
